package com.zjdiepu.www.rest.api.dz;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Id;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.ParentModel;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.BusinessExtrasCodeType;
import com.zjdiepu.www.base.enums.type.BusinessExtrasType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.commpent.handler.TokenUserHelper;
import com.zjdiepu.www.ibiz.dz.IDzGpsService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessExtrasService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.yw.dto.YwBusinessExtras;
import com.zjdiepu.www.model.yw.request.ChangeGpsRequest;

/**
 * Controller层<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年06月01日 19时14分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "DzGpsController", produces="application/json;charset=UTF-8",method = RequestMethod.POST)
@Api(tags = {"GPS信息"}, produces="application/json;charset=UTF-8")
public class DzGpsController extends BaseController {
	@Resource
	private TokenUserHelper tokenUserHelper;
    @Resource
    private IDzGpsService dzGpsService;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private IYwBusinessExtrasService ywBusinessExtrasService;

    /**
     * @author xiongxiaotun <br/>
     * @Title: changeGps  <br/>
     * @Description: 修改GPS信息 <br/>
     * @param changeChannelRequest
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改gps信息", response = Boolean.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("changeGps")
    public ApiResponse changeGps(@Validated(Update.class) @RequestBody ChangeGpsRequest changeGpsRequest) {
    	SysUser currentUser = tokenUserHelper.getUser();
    	dzGpsService.saveChangeGps(changeGpsRequest, currentUser);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: getGps  <br/>
     * @Description: 获取GPS信息 <br/>
     * @param parentModel
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "获取GPS信息", response = ChangeGpsRequest.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("getGps")
    public ApiResponse getGps(@Validated(Id.class) @RequestBody ParentModel parentModel) {
    	tokenUserHelper.getUser();
    	String busiId = parentModel.getId();
    	ChangeGpsRequest changeGpsRequest = new ChangeGpsRequest();
    	changeGpsRequest.setId(busiId);
    	
    	//判断是否已经保存
		YwBusinessExtras ywBusinessExtras_ = new YwBusinessExtras();
		ywBusinessExtras_.setBusiId(busiId);
		ywBusinessExtras_.setTypeExtras(BusinessExtrasType.AUDIT.getCode());
		ywBusinessExtras_.setCodeExtras(BusinessExtrasCodeType.ADD_GPS.getCode());
		ywBusinessExtras_.setDpStatus(DpStatus.NORMAL.getCode());
		ywBusinessExtras_ = ywBusinessExtrasService.selectRefined(ywBusinessExtras_);
		if(null != ywBusinessExtras_) {
			String valueExtras = ywBusinessExtras_.getValueExtras();
			changeGpsRequest.setValueExtras(valueExtras);
		}
        return ApiResponse.success(changeGpsRequest);
    }
}
